/*--------------------------------------------------------------------------
 *
 * 	You may freely copy, distribute, and reuse the code in this example.
 * 	SHL Systemhouse disclaims any warranty of any kind, expressed or  
 *	implied, as to its fitness for any particular use.
 *
 *
 *	Database
 *
 *	Category Of:	EOFDelegate
 *
 *	Conforms To:	None
 *
 *	Declared In:	EOFDelegateDatabaseCategory.h
 *
 *
 *------------------------------------------------------------------------*/
#import "EOFDelegateDatabaseCategory.h"




@implementation EOFDelegate (Database)

/*--------------------------------------------------------------------------
 *	EODatabaseChannel Delegate Methods
 *------------------------------------------------------------------------*/
- databaseChannel:channel willInsertObject:object
{
    // Invoked from -insertObject: to tell the delegate that an object will
    // be inserted. The delegate may return the object, a substitute, or nil
    // to prevent insertion.
	
	id	result = object;
	
	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObject:object]];

	if ([[NXApp delegate] wantsAlertPanels] == YES)
		{
		switch (NXRunAlertPanel ("EODatabaseChannel", 
			"%s", "YES", "NO", NULL, sel_getName(_cmd)))
			{
			case NX_ALERTALTERNATE:
				result = nil;
				break;
			case NX_ALERTDEFAULT:
			default:
				result = object;
				break;
			}
		}
	
	return result;
}


- (void)databaseChannel:channel didInsertObject:object
{
    // Invoked from -insertObject: to tell the delegate that 
    // an object was inserted.

	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObject:object]];
}


- databaseChannel:channel willDeleteObject:object
{
    // Invoked from -deleteObject: to tell the delegate that an object will
    // be deleted.  The delegate may return the object, a substitute, or nil
    // to prevent deletion.
	
	id	result = object;
	
	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObject:object]];
	
	if ([[NXApp delegate] wantsAlertPanels] == YES)
		{
		switch (NXRunAlertPanel ("EODatabaseChannel", 
			"%s", "YES", "NO", NULL, sel_getName(_cmd)))
			{
			case NX_ALERTALTERNATE:
				result = nil;
				break;
			case NX_ALERTDEFAULT:
			default:
				result = object;
				break;
			}
		}
	
	return result;
}


- (void)databaseChannel:channel didDeleteObject:object
{
    // Invoked from -deleteObject: to tell the delegate that 
    // an object was deleted.

	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObject:object]];
}


- databaseChannel:channel willUpdateObject:object
{
    // Invoked from -updateObject: to tell the delegate that an object will
    // be updated.  The delegate may return the object, a substitute, or nil
    // to prevent updating.
	
	id	result = object;
	
	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObject:object]];
	
	if ([[NXApp delegate] wantsAlertPanels] == YES)
		{
		switch (NXRunAlertPanel ("EODatabaseChannel", 
			"%s", "YES", "NO", NULL, sel_getName(_cmd)))
			{
			case NX_ALERTALTERNATE:
				result = nil;
				break;
			case NX_ALERTDEFAULT:
			default:
				result = object;
				break;
			}
		}
	
	return result;
}


- (void)databaseChannel:channel didUpdateObject:object
{
    // Invoked from -updateObject: to tell the delegate that 
    // an object was updated.

	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObject:object]];
}


- (NSDictionary *)databaseChannel:channel
    willRefetchObject:object
    fromSnapshot:(NSDictionary *)snapshot
{
    // Invoked whenever an object that was previously fetched is fetched again
    // and the re-fetched data is different from that in the object.  The
    // object will be refetched with the dictionary returned by this method,
    // which may be the snapshot provided or a substitute.  The delegate may
    // also return nil to prevent the object from being refetched.
	
	id	result = object;
	
	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObjects: object, snapshot, nil]];
	
	if ([[NXApp delegate] wantsAlertPanels] == YES)
		{
		switch (NXRunAlertPanel ("EODatabaseChannel", 
			"%s", "YES", "NO", NULL, sel_getName(_cmd)))
			{
			case NX_ALERTALTERNATE:
				result = nil;
				break;
			case NX_ALERTDEFAULT:
			default:
				result = object;
				break;
			}
		}
	
	return result;
}



- (BOOL)databaseChannel:channel
    willSelectObjectsDescribedByQualifier:(EOQualifier *)qualifier
    fetchOrder:(NSArray *)fetchOrder
{
    // Invoked from -selectObjectsDescribedByQualifier:...  to tell the
    // delegate that the channel will select objects as specified by
    // qualifier.  The delegate can modify the qualifier and fetch order.  If
    // the delegate returns YES the channel will go ahead and select the
    // object; if the delegate returns NO the channel will abort the select
    // and return NO.
	
	BOOL	result = YES;
	
	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObjects: qualifier, fetchOrder, nil]];
	
	if ([[NXApp delegate] wantsAlertPanels] == YES)
		{
		switch (NXRunAlertPanel ("EODatabaseChannel", 
			"%s", "YES", "NO", NULL, sel_getName(_cmd)))
			{
			case NX_ALERTALTERNATE:
				result = NO;
				break;
			case NX_ALERTDEFAULT:
			default:
				result = YES;
				break;
			}
		}
	
	return result;
}


- (void)databaseChannel:channel
    didSelectObjectsDescribedByQualifier:(EOQualifier *)qualifier
    fetchOrder:(NSArray *)fetchOrder
{
    // Invoked from -selectObjectsDescribedByQualifier:...  to tell the
    // delegate that the channel selected objects as specified by qualifier.

	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObjects: qualifier, fetchOrder, nil]];
}


- (void)databaseChannel:channel
    willFetchObjectOfClass:(Class)class
    withZone:(NSZone *)zone
{
    // Invoked from -fetchWithZone: to tell the delegate that 
    // the channel willfetch the next selected object.

	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObject:class]];
}


- (void)databaseChannel:channel didFetchObject:object
{
    // Invoked from -fetchWithZone: to tell the delegate that 
    // the channel fetched the next selected object.
	
	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObject:object]];
}


- (BOOL)databaseChannel:channel willLockObject:object
{
    // Invoked from -lockObject: to tell the delegate that object will be
    // locked using snapshot data to determine whether the database has been
    // modified.  The delegate may return YES to allow the channel to proceed,
    // or NO to cause the operation to fail and -lockObject: to return NO.
	
	BOOL	result = YES;
	
	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObject:object]];
	
	if ([[NXApp delegate] wantsAlertPanels] == YES)
		{
		switch (NXRunAlertPanel ("EODatabaseChannel", 
			"%s", "YES", "NO", NULL, sel_getName(_cmd)))
			{
			case NX_ALERTALTERNATE:
				result = NO;
				break;
			case NX_ALERTDEFAULT:
			default:
				result = YES;
				break;
			}
		}
	
	return result;
}


- (void)databaseChannel:channel didLockObject:object
{
    // Invoked from -lockObject: to tell the delegate that 
	// object was locked.

	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObject:object]];
}


@end
